/*
 * ToneGenApp.java
 */
package tonegen;

import acelab.bluetooth.*;
import java.io.File;
import java.io.IOException;
import java.util.LinkedList;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import org.jdesktop.application.Application;
import org.jdesktop.application.SingleFrameApplication;

/**
 * The main class of the application.
 */
public class ToneGenApp extends SingleFrameApplication {

    DeviceCommunication accelerometer = new DeviceCommunication(3);
    //      static SoundOutput sound;

    /**
     * At startup create and show the main frame of the application.
     */
    @Override
    protected void startup() {
        show(new ToneGenView(this));
    }

    /**
     * This method is to initialize the specified window by injecting resources.
     * Windows shown in our application come fully initialized from the GUI
     * builder, so this additional configuration is not needed.
     */
    @Override
    protected void configureWindow(java.awt.Window root) {
    }

    /**
     * A convenient static getter for the application instance.
     * @return the instance of ToneGenApp
     */
    public static ToneGenApp getApplication() {
        return Application.getInstance(ToneGenApp.class);
    }

    void start(ToneGenView aThis) {
        accelerometer.start(aThis);
    }

    public void stop() {
        accelerometer.stop();
    }

    public boolean save() {
        JFileChooser fc = new JFileChooser();
        fc.setCurrentDirectory(new File(System.getenv("USERPROFILE")));
        System.out.println("Selecting file...");
        if (JFileChooser.APPROVE_OPTION == fc.showSaveDialog(this.getMainFrame())) {

            File file = fc.getSelectedFile();
            if (!file.exists()) {
                try {
                    file.createNewFile();
                } catch (IOException ex) {
                    JOptionPane.showMessageDialog(this.getMainFrame(),
                            "Choose a different file.",
                            "File could not be created.",
                            JOptionPane.WARNING_MESSAGE);
                    return false;
                }
            }
            accelerometer.save(file.getPath());
            return true;
        } else {
            return false;
        }
    }

    public void setThresholds(LinkedList<Double> t, LinkedList<Double> f) {
        accelerometer.setThresholds(t, f);
        accelerometer.saveConfig(t, f);
    }
    
    public void loadConfig() {
        accelerometer.loadConfig();
    }

    /**
     * Main method launching the application.
     */
    public static void main(String[] args) {

        launch(ToneGenApp.class, args);
        System.out.println("Connecting to bluetooth device");
    }
}
